package example;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.RadioMenuItemBuilder;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class DevelopingADialog extends Application{

	static Stage LOGIN_DIALOG;
	static int dx = 1;
	static int dy = 1;
	
	public static void main(String[] args) {
		launch(args);

	}

	private static Stage createLoginDialog(Stage parent, boolean modal){//modal：模式
		if(LOGIN_DIALOG != null){
			LOGIN_DIALOG.close();
		}
		return new MyDialog(parent,modal,"WelCome to javaFX");
		
	}
	@Override
	public void start(final Stage primaryStage) throws Exception {
		primaryStage.setTitle("Chapter 1-17 Developing a Dialog");
        Group root = new Group();
        Scene scene = new Scene(root, 433, 312);

        MenuBar menuBar = new MenuBar();
        menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
        
        Menu menu = new Menu("Home");

        // add change password menu itme
        MenuItem newItem = new MenuItem("Change Password", null);
        newItem.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent event) {
				if(LOGIN_DIALOG == null){
					LOGIN_DIALOG = createLoginDialog(primaryStage, true);
				}
				LOGIN_DIALOG.sizeToScene();
				LOGIN_DIALOG.show();		
			}
        	
        });
        
        menu.getItems().add(newItem);

        // add separator
        menu.getItems().add(new SeparatorMenuItem());
        
     // add non modal menu item
        ToggleGroup modalGroup = new ToggleGroup();
        RadioMenuItem nonModalItem = RadioMenuItemBuilder.create()
                .toggleGroup(modalGroup)
                .text("Non Modal")
                .selected(true)
                .build();
        nonModalItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                LOGIN_DIALOG = createLoginDialog(primaryStage, false);
            }
        });

        menu.getItems().add(nonModalItem);

        // add modal selection
        RadioMenuItem modalItem = RadioMenuItemBuilder.create()
                .toggleGroup(modalGroup)
                .text("Modal")
                .selected(true)
                .build();
        modalItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                LOGIN_DIALOG = createLoginDialog(primaryStage, true);
            }
        });
        menu.getItems().add(modalItem);

        // add separator
        menu.getItems().add(new SeparatorMenuItem());

        // add exit
        MenuItem exitItem = new MenuItem("Exit", null);
        exitItem.setMnemonicParsing(true);
        exitItem.setAccelerator(new KeyCodeCombination(KeyCode.X, KeyCombination.CONTROL_DOWN));
        exitItem.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                Platform.exit();
            }
        });
        menu.getItems().add(exitItem);
        
     // add menu
        menuBar.getMenus().add(menu);
		
     // menu bar to window
        root.getChildren().add(menuBar);

        primaryStage.setScene(scene);
        primaryStage.show();
        
        addBouncyBall2(scene);
	}
	
	private void addBouncyBall(final Scene scene){
		final Circle ball = new Circle(100,100,20);
		RadialGradient gradient1 = new RadialGradient(
				0,
                .1,
                100,
                100,
                20,
                false,
                CycleMethod.NO_CYCLE,
                new Stop(0, Color.WHITE),
                new Stop(1, Color.GREEN));
		ball.setFill(gradient1);
		
		final Group root = (Group) scene.getRoot();
        root.getChildren().add(ball);
        
        Timeline tl = new Timeline();
        tl.setCycleCount(Animation.INDEFINITE);//indefinite:不确定的。
        
        KeyFrame moveBall = new KeyFrame(Duration.seconds(.01500),
        		new EventHandler<ActionEvent>(){

					@Override
					public void handle(ActionEvent event) {
						double xMin = ball.getBoundsInParent().getMinX();
						double yMin = ball.getBoundsInParent().getMinY();
                        double xMax = ball.getBoundsInParent().getMaxX();
                        double yMax = ball.getBoundsInParent().getMaxY();
                        
                        if(xMin<0 || xMax>scene.getWidth()){
                        	dx = dx * -1;
                        }
                        if (yMin < 0 || yMax > scene.getHeight()) {
                            dy = dy * -1;
                        }
                        
                        ball.setTranslateX(ball.getTranslateX() + dx);
                        ball.setTranslateY(ball.getTranslateY() + dy);
						
					}
        		});
        tl.getKeyFrames().add(moveBall);
        tl.play();
	}
	private void addBouncyBall2(final Scene scene){
		final Circle ball = new Circle(100,100,20);
		RadialGradient gradient1 = new RadialGradient(
				0,
                .1,
                100,
                100,
                20,
                false,
                CycleMethod.NO_CYCLE,
                new Stop(0, Color.WHITE),
                new Stop(1, Color.GREEN));
		ball.setFill(gradient1);
		
		final Group root = (Group) scene.getRoot();
        root.getChildren().add(ball);
        
        Timeline tl = new Timeline();
        tl.setCycleCount(Animation.INDEFINITE);//indefinite:不确定的。
        
        KeyFrame moveBall = new KeyFrame(Duration.seconds(1.01500),
        		new EventHandler<ActionEvent>(){

					@Override
					public void handle(ActionEvent event) {
						double xMin = ball.getBoundsInParent().getMinX();
						double yMin = ball.getBoundsInParent().getMinY();
                        double xMax = ball.getBoundsInParent().getMaxX();
                        double yMax = ball.getBoundsInParent().getMaxY();
                        
                        if(xMin<0 || xMax>scene.getWidth()){
                        	dx = dx * -5;
                        }
                        if (yMin < 0 || yMax > scene.getHeight()) {
                            dy = dy * -5;
                        }
                        
                        ball.setTranslateX(ball.getTranslateX() + dx);
                        ball.setTranslateY(ball.getTranslateY() + dy);
						
					}
        		});
        KeyFrame moveBall2 = new KeyFrame(Duration.seconds(1.01500),
        		new EventHandler<ActionEvent>(){

					@Override
					public void handle(ActionEvent event) {
						double xMin = ball.getBoundsInParent().getMinX();
						double yMin = ball.getBoundsInParent().getMinY();
                        double xMax = ball.getBoundsInParent().getMaxX();
                        double yMax = ball.getBoundsInParent().getMaxY();
                        
                        if(xMin<0 || xMax>scene.getWidth()){
                        	dx = dx * +15;
                        }
                        if (yMin < 0 || yMax > scene.getHeight()) {
                            dy = dy * +15;
                        }
                        
                        ball.setTranslateX(ball.getTranslateX() - dx);
                        ball.setTranslateY(ball.getTranslateY() -dy);
						
					}
        		});
        tl.getKeyFrames().add(moveBall);
        tl.getKeyFrames().add(moveBall2);
        
        tl.play();
	}
}
